package com.xidian.zhiancloud.controller.workTicket;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dao.SysJobMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.workTicket.FireFlowMapper;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.dto.workTicket.FireStaffDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.SysJob;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.model.workTicket.FireFlow;
import com.xidian.zhiancloud.service.workTicket.FireFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("FireFlow")
public class FireFlowController extends BaseController {
    @Autowired
    private FireFlowService fireFlowService;

    @Autowired
    private FireFlowMapper fireFlowMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysJobMapper sysJobMapper;

    // 人员设置接口：前端传进来动火作业的subId，<选择标准，具体ids>，即Map<Integer, String>
    @PostMapping("/setFireStaff")
    public ResultDTO<Boolean> setFireStaff(Integer subId, Integer selectStandard, String selectPerson) {
        Long companyId = getSessionUserCompanyId();
        // 获取fireFlowid
        Long fireFlowId = fireFlowService.getFlowBySubId(companyId, subId);
        FireFlow fireFlow = fireFlowService.getById(fireFlowId);
        // 如果当前数据的字段已经非空,且与新填入的不同，则判错
        if ((fireFlow.getSelectStandard()!= null) && (fireFlow.getSelectStandard() != selectStandard)) {
            return ResultDTO.errorOf(GeneralResponseEnums.SUBMIT_FAILED);
        } else {
            fireFlow.setSelectStandard(selectStandard);
        }
        // 如果当前selectPerson非空，则加","
        if (fireFlow.getSelectPerson() != null && fireFlow.getSelectPerson().length() != 0) {
            selectPerson = fireFlow.getSelectPerson() + ","+ selectPerson;
        }
        fireFlow.setSelectPerson(selectPerson);
        Boolean res = fireFlowService.updateById(fireFlow);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }


    // 查看人员设置接口：返回人员和职位的中文名称
    @GetMapping("getFireStaff")
    public ResultDTO<FireStaffDTO> getFireStaff() {
        Long companyId = getSessionUserCompanyId();
        String PersonList = "";
        // 返回值类型
        FireStaffDTO res = new FireStaffDTO();
        // 名字
        Map<Integer, List<String>> fireflowMap = new LinkedHashMap<>();
        // id
        Map<Integer, List<Long>> fireflowIds = new LinkedHashMap<>();
        // 选择标准
        List<Integer> standards = new LinkedList<>();
        LambdaQueryWrapper<FireFlow> query = Wrappers.<FireFlow>lambdaQuery();
        query.eq(FireFlow::getCompanyId, companyId);
        // 获取一个公司，所有的15条fireFLow记录
        List<FireFlow> fireFlows = fireFlowMapper.selectList(query);
        // 遍历，填充map
        for (int i = 1; i < fireFlows.size(); i++) {
            // 查找人员列表
            String ids_s = fireFlows.get(i).getSelectPerson();
            // 判断是通过人员 / 职位 进行id ——> string
            if (fireFlows.get(i).getSelectStandard() == 0) {        // 人员
                long[] ids_l = Arrays.stream(ids_s.split(",")).mapToLong(Long::parseLong).toArray();
                List<String> names = Arrays.stream(ids_l).mapToObj(tmp_id -> {
                    SysUser workers = sysUserMapper.selectById(tmp_id);
                    return workers == null ? "该用户已被删除" : workers.getName();
                }).collect(Collectors.toList());
                fireflowMap.put(i+1, names);

                // 添加人员id
                List<Long> ids = new LinkedList<>();
                if(ids_l!=null && ids_l.length!=0){
                    for (int i1 = 0; i1 < ids_l.length; i1++) {
                        ids.add(ids_l[i1]);
                    }
                }
                fireflowIds.put(i+1, ids);
            } else {                                                // 职位
                long[] ids_l = Arrays.stream(ids_s.split(",")).mapToLong(Long::parseLong).toArray();
                List<String> names = Arrays.stream(ids_l).mapToObj(tmp_id -> {
                    SysJob sysJob = sysJobMapper.selectById(tmp_id);
                    return sysJob == null ? "该职位已被删除" : sysJob.getPostName();
                }).collect(Collectors.toList());
                // 添加职位id
                List<Long> ids = new LinkedList<>();
                if(ids_l!=null && ids_l.length!=0){
                    for (int i1 = 0; i1 < ids_l.length; i1++) {
                        ids.add(ids_l[i1]);
                    }
                }
                fireflowMap.put(i+1, names);
            }
            standards.add(fireFlows.get(i).getSelectStandard());
        }
        res.setStaffNames(fireflowMap);
        res.setStaffIds(fireflowIds);
        res.setSelectStandards(standards);

        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }


}
